#!/usr/bin/env bash

requirements_pclinuxos_lib_installed()
{
  rpm -q --whatprovides "$1" >/dev/null || return $?
  # Alternative: rpm -qa | grep "$1" >/dev/null || return $?
}

requirements_pclinuxos_lib_available()
{
  apt-cache search "$1" >/dev/null 2>&1 || return $?
}

requirements_pclinuxos_version_list()
{
  rpm -q --whatprovides "$1" | sed -e "s#^$1-##"
}

requirements_pclinuxos_libs_install()
{
  __rvm_try_sudo apt-get -y install "$@" || return $?
}

requirements_pclinuxos_update_system()
{
  true
}

requirements_pclinuxos_check_binary()
{
  {
    __rvm_which update-alternatives >/dev/null &&
    update-alternatives --display "$1" >/dev/null 2>&1
  } ||
  __rvm_which "$1" >/dev/null || return $?
}

requirements_pclinuxos_define()
{
  case "$1" in
    (rvm)
      requirements_check bash curl patch
      ;;

    (jruby*)
      requirements_check make

      if
        is_head_or_disable_binary "$1"
      then
        requirements_pclinuxos_check_binary javac ||
          requirements_check_fallback java-1.7.0-openjdk-devel java-devel

        requirements_check git

        case $( jruby_installation_method  "$1" ) in
          mvn) requirements_check_custom_after mvn=maven ;;
        esac
      else
        requirements_pclinuxos_check_binary java ||
          requirements_check_fallback java-1.7.0-openjdk java
      fi
      ;;

    (ir*)
      true # not that easy
      ;;

    (opal)
      true # not that easy
      ;;

    (rbx*|rubinius*)
      requirements_check clang clang-devel llvm llvm-devel
      ;;

    (truffleruby*)
      requirements_check lib64openssl-devel zlib zlib-devel make gcc libxml2 libyaml-devel
      ;;

    (*-head)
      requirements_check git
      requirements_pclinuxos_define "${1%-head}"
      ;;

    (*)
      if [[ ${#rvm_patch_names[@]} -gt 0 || "$1" == *"-head" ]]
      then requirements_version_minimal autoconf 2.67
      else requirements_check autoconf
      fi

      case "${_system_arch}" in
        (x86_64)
          requirements_check lib64yaml-devel lib64ffi-devel lib64readline-devel lib64openssl-devel
          ;;
        (*)
          requirements_check libyaml-devel libffi5-devel readline-devel libopenssl-devel
          ;;
      esac

      requirements_check automake bison bzip2 gcc-c++ glibc-devel glibc-headers-devel libtool make patch \
         zlib zlib-devel
      ;;
  esac
}
